﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;

namespace AUI
{
    public partial class frmUserInfo : FormBase
    {
        int _EmployeeID = 0;
        public frmUserInfo()
        {
            InitializeComponent();
        }

        DataRow m_dataRow;
        public DialogResult Show_Form(DataRow datarow)
        {
            this.txtUserName.MaxLength = tbl_Users_Length.UserName;
            this.txtPassword.MaxLength = tbl_Users_Length.Password;
            this.txtConfirmPassword.MaxLength = tbl_Users_Length.Password;

            _listRole = tbl_RoleRight_Controller.Instance().GetInfo("NguoiDung");
            if (datarow != null)
            {
                txtUserName.ReadOnly = true;

                _userName = Globals.ToString(datarow["userName"]);
                _EmployeeID = Globals.ToInt32(datarow["EmployeeId"]);

                //Get employeCode,employeeName...
                FormGlobals.Panel_FillData(this, datarow);

                tbl_Users_Info Info = tbl_Users_Controller.Instance().GetInfo(_userName);
                //Get password, email...
                FormGlobals.Panel_FillData(this, Info);

                txtConfirmPassword.Text = txtPassword.Text;

                FormGlobals.dgv_SelectionMode_FullRowSelect(dgvRoles);
                dgvRoles.BorderStyle = BorderStyle.FixedSingle;
                FormGlobals.dgv_AddColumn_TextBox(dgvRoles, "RoleName", "Tên vai trò", "", 1, true, true);
                FormGlobals.dgv_ColumnsSortMode(dgvRoles, DataGridViewColumnSortMode.NotSortable);
                dgvRoles.Columns["RoleName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                string[] Roles = tbl_Roles_Controller.Instance().GetRolesForUser(_userName);
                DataTable dtRole = new DataTable();
                dtRole.Columns.Add("RoleName", typeof(string));
                foreach (string item in Roles)
                {
                    dtRole.Rows.Add(item);
                }

                dgvRoles.DataSource = dtRole;
           
            }
            else
            {
                grpRoles.Visible = false;
                this.Height = 269;
            }
            m_dataRow = datarow;
            return this.ShowDialog();
        }
        public override void Show_Form()
        {
            this.ShowDialog();
        }
        protected override void Init_Control()
        {
            //MessageBox.Show("dddddddddđ");
        }
        private bool CheckValid()
        {
            if (_EmployeeID == 0)
            {
                FormGlobals.Message_Warning("Bạn chưa chọn nhân viên");
                return false;
            }

            if (!txtUserName.ReadOnly)
            {
                txtUserName.BackColor = SystemColors.Window;
                if (txtUserName.Text.Trim().Length == 0)
                {
                    txtUserName.BackColor = Color.Yellow;
                    FormGlobals.Message_Warning("Tên đăng nhập trống");
                    txtUserName.Focus();
                    return false;

                }
                else if (_userName == "" && tbl_Users_Controller.Instance().IsExist(txtUserName.Text.Trim()))
                {
                    txtUserName.BackColor = Color.Yellow;
                    FormGlobals.Message_Warning("Tên đăng nhập đã được cấp, hãy cấp tên khác!");
                    txtUserName.Focus();
                    return false;
                }
            }
            txtPassword.BackColor = SystemColors.Window;
            if (txtPassword.Text.Trim().Length == 0)
            {
                txtPassword.BackColor = Color.Yellow;
                FormGlobals.Message_Warning("Mật khẩu trống");
                txtPassword.Focus();
                return false;
            }
            if (txtPassword.Text.Trim().Length < 7)
            {
                txtPassword.BackColor = Color.Yellow;
                FormGlobals.Message_Warning("Mật khẩu phải có ít nhất là 7 ký tự");
                txtPassword.Focus();
                return false;
            }
            txtConfirmPassword.BackColor = SystemColors.Window;
            if (!txtPassword.Text.Equals(txtConfirmPassword.Text))
            {
                txtConfirmPassword.BackColor = Color.Yellow;
                FormGlobals.Message_Warning("Mật khẩu mới và mật khẩu cũ không trùng hơp");
                txtConfirmPassword.Focus();
                return false;
            }
            
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckValid())
                {
                    return;
                }


                tbl_Users_Info i = new tbl_Users_Info();
                FormGlobals.Panel_GetData(this, i);
                i.EmployeeId = _EmployeeID;

                if (_userName.Trim().Length > 0)
                {
                    

                    if (tbl_Users_Controller.Instance().Update(i).Length == 0)
                    {
                        if (FormGlobals.strUserName.ToLower().Equals(_userName.ToLower()))
                        {
                            //Khởi tạo lại service sau khi đã đổi mật khẩu
                            FormGlobals.strPassword = Globals.HashPassword(txtPassword.Text.Trim());
                        }
                    }
                }
                else
                {
                   string retVal= tbl_Users_Controller.Instance().Create(i);
                    _userName = i.UserName;
                    txtUserName.ReadOnly = true;

                }

                FormGlobals.Message_InTaskbar(UIMessage.SAVE_COMPLETE);
                _dlgResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Error(ex);
            }

        }

        private void btnSelect_Employee_Click(object sender, EventArgs e)
        {
            frmEmployeesList f = new frmEmployeesList("Chọn nhân viên", true, 0, "");
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtEmployeeCode.Text = f.EmployeeCode;
                txtEmployeeName.Text = f.EmployeeName;
                _EmployeeID = f.EmployeeId;
            }
            this.Activate();

        }

    }
}
